package com.jx.lcs.datareport.web;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jx.lcs.datareport.service.DataReportService;
import com.tianyu.jty.common.utils.DateUtils;
import com.tianyu.jty.common.web.BaseController;

/**
 * 数据报表
 * 
 * @author JiaYY
 * @date 2015年7月3日
 */

@Controller
@RequestMapping("dataReport")
public class DataReportController extends BaseController {

	private static final Logger logger = LoggerFactory
			.getLogger(DataReportController.class);

	@Autowired
	private DataReportService dataReportService;

	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "report/reportIndex";
	}

	/**
	 * 用户相关数据报表
	 */
	@RequestMapping(value = "userDataReport", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> userDataReport(String beginDate, String endDate,
			HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> userData = dataReportService
					.findUserDataReport(beginDate, endDate);
			map.put("total", 0);
			map.put("rows", userData);
		} catch (Exception e) {
			logger.error("--获取用户相关数据报表出现异常！", e);
		}
		return map;
	}

	/**
	 * 产品相关数据报表
	 */
	@RequestMapping(value = "productDataReport", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findProductDataReport(String beginDate, String endDate,
			HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> productData = dataReportService
					.findProductDataReport(beginDate, endDate, request);

			map.put("total", 0);
			map.put("rows", productData);
		} catch (Exception e) {
			logger.error("--获取产品相关数据报表出现异常！", e);
		}
		return map;
	}

	/**
	 * 报表导出
	 * 
	 * @param beginDate
	 *            开始日期
	  * @param beginDate
	 *            结束日期
	 * @param response
	 *            HttpServletResponse
	 * @throws IOException
	 *             网络问题会出发IO异常
	 */
	@RequestMapping(value = "export", method = RequestMethod.GET, produces = "text/plain;charset=utf-8")
	@ResponseBody
	public void export(String beginDate, String endDate, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		String beginStr = null;
		if(!StringUtils.isBlank(beginDate)) {
			beginStr = DateUtils.formatDate(DateUtils.parseDate(beginDate), "yyyyMMdd");
		} 
		
		String endStr = null;
		if(!StringUtils.isBlank(endDate)) {
			endStr = DateUtils.formatDate(DateUtils.parseDate(endDate), "yyyyMMdd");
		} else {
			endStr = DateUtils.formatDate(DateUtils.getSysDate(), "yyyyMMdd");
		}
		
		String fileName = "";
		if(beginStr != null) {
			fileName = beginStr + "-" + endStr;
		} else {
			fileName = "-" + endStr;
		}
		
		response.setContentType("text/html;charset=utf-8");
		response.setContentType("application/x-msdownload;");
		response.setHeader("Content-disposition", "attachment; filename="
				+ URLEncoder.encode("无忧理财用户和产品相关使用情况数据" + fileName, "UTF-8") + ".xls");

		dataReportService.exportDataReport(beginDate, endDate, request,
				response.getOutputStream());

	}

}
